<?php

class TournamentsPage extends BaseAdminPage {

	/**
	 * Az összes rekord
	 *
	 * @var array
	 */
	private $allRecords;

	function TournamentsPage() {
		parent::BaseAdminPage('TournamentsPage');
	}

	function enterPage() {
		parent::enterPage();

		$langcodes = AdminBase::getEditLanguages();

		// definiáljuk a form elemeit
		FormUtil::addForm("newtournament");
		FormUtil::addFormElement("newtournament", new FormElementInfo("newname", "text"));
		FormUtil::addFormElement("newtournament", new FormElementInfo("newlocation", "text"));
		FormUtil::addFormElement("newtournament", new FormElementInfo("newdatefrom", "date", null, array("isTime" => false)));
		FormUtil::addFormElement("newtournament", new FormElementInfo("newdateto", "date", null, array("isTime" => false)));
		FormUtil::addFormElement("newtournament", new FormElementInfo("newemail", "text"));
		FormUtil::addFormElement("newtournament", new FormElementInfo("newdefault_langcode", "select", 'en', array('arrayType' => 'objectArray', 'array' => $langcodes, 'labelField' => 'langName')));
		FormUtil::resetFormValues("newtournament");

		// definiáljuk a form elemeit
		$compTypes = TournamentHandler::getTournamentTypesForCombo();
		$compConfigs = TournamentHandler::getTournamentConfigurationsForCombo();
		FormUtil::addForm("tournament");
		FormUtil::addFormElement("tournament", new FormElementInfo("id", "text"));
		FormUtil::addFormElement("tournament", new FormElementInfo("name", "text"));
		FormUtil::addFormElement("tournament", new FormElementInfo("location", "text"));
		FormUtil::addFormElement("tournament", new FormElementInfo("date_from", "date", null, array("isTime" => false)));
		FormUtil::addFormElement("tournament", new FormElementInfo("date_to", "date", null, array("isTime" => false)));
		FormUtil::addFormElement("tournament", new FormElementInfo("deadline", "date", null, array("isTime" => false)));
		FormUtil::addFormElement("tournament", new FormElementInfo("email", "text"));
		FormUtil::addFormElement("tournament", new FormElementInfo("default_langcode", "select", 'en', array('arrayType' => 'objectArray', 'array' => $langcodes, 'labelField' => 'langName')));
		FormUtil::addFormElement("tournament", new FormElementInfo("types", "select", null, array('arrayType' => 'simpleArray', 'array' => $compTypes, "translateLabels"=>false)));
		FormUtil::addFormElement("tournament", new FormElementInfo("configuration", "select", null, array('arrayType' => 'simpleArray', 'array' => $compConfigs, "translateLabels"=>false)));
		FormUtil::resetFormValues("tournament");
	}

	function leavePage() {
		parent::leavePage();
			
		FormUtil::removeAllForms();
	}

	public function serveRequest() {
		parent::serveRequest();

		CalendarUtil::loadCalendarJavascriptAndCss(TextHandler::getCurrentLangcode());

		$tpl = BaseAdminPage::loadTemplateFile("tournament.tpl.html");
		TextHandler::insertTextsIntoTemplate($tpl);

		$tpl->setVariable('app_root_url', APP_ROOT_URL);

		$this->allRecords = AdminDBHelper::getTournaments(AdminBase::getCurrentEditlang());
		if(is_null($this->allRecords)) {
			// suxxx!
			handleError(TextHandler::getText('err_readrecordsfailed'));
			$tpl->replaceBlock('tournament', ' ', false);
			$tpl->touchBlock('tournament');
		} else {
			// megvannak a rekordok!

			$action = FunctionLibrary::getGetValue('action');
			if($action) {
				$action = 'action_'.$action;
				$this->$action();
			}

			// kell a hozzáadás művelet
			FormUtil::insertFormValuesToTemplate("newtournament", $tpl);
			$tpl->setVariable('addFormAction', HydraLinkProvider::getLink('addTournament'));

			if(count($this->allRecords) == 0) {
				$tpl->replaceBlock('tournament', ' ', false);
				$tpl->touchBlock('tournament');
			} else {
				foreach($this->allRecords as $record) {
					$tpl->setCurrentBlock("tournament");

					if(FormUtil::hasFormInstance("tournament", $record['id'])) {
						// szerkesztés alatt áll
						$contentTplHtml = "tournament_edit.tpl.html";
					} else {
						// nem áll szerkesztés alatt
						$contentTplHtml = "tournament_raw.tpl.html";
					}
					$contentTpl = BaseAdminPage::loadTemplateFile($contentTplHtml);
					TextHandler::insertTextsIntoTemplate($contentTpl);

					$contentTpl->setVariable('app_root_url', APP_ROOT_URL);

					$trClasses = array();

					$contentTpl->setVariable("link_del", HydraLinkProvider::getLink("delTournament", array("id" => $record['id'])));
					$contentTpl->setVariable("tr_class", implode($trClasses, " "));

					$contentTpl->setVariable("id", $record['id']);

					if(FormUtil::hasFormInstance("tournament", $record['id'])) {
						// szerkesztés alatt áll
						FormUtil::insertFormValuesToTemplate('tournament', $contentTpl, $record['id']);

						$contentTpl->setVariable("formAction", HydraLinkProvider::getLink("modTournament", array("id" => $record['id'])));
						$contentTpl->setVariable("link_cancel", HydraLinkProvider::getLink("cancelEditTournament", array("id" => $record['id'])));
					} else {
						// nem áll szerkesztés alatt
						if ($record['langcode'] == AdminBase::getCurrentEditlang()) {
							$contentTpl->setVariable("name", $record['name']);
						} else {
							$contentTpl->setVariable("name", '['.$record['name'].'] *');
						}
						$contentTpl->setVariable("location", $record['location']);
						$contentTpl->setVariable("datefrom", TournamentHandler::formatDate($record['date_from']));
						$contentTpl->setVariable("dateto", TournamentHandler::formatDate($record['date_to']));
						$contentTpl->setVariable("deadline", TournamentHandler::formatDate($record['deadline']));
						$contentTpl->setVariable("email", $record['email']);
						$contentTpl->setVariable("link_edit", HydraLinkProvider::getLink("editTournament", array("id" => $record['id'])));
						$contentTpl->setVariable("link_editarticles", HydraLinkProvider::getLink("editTournamentArticles", array("id" => $record['id'])));
						$contentTpl->setVariable("link_editregistrations", HydraLinkProvider::getLink("editTournamentRegistrations", array("id" => $record['id'])));
					}
					$tpl->setVariable('tournamement', $contentTpl->get());
					$contentTpl = null;

					$tpl->parseCurrentBlock();
				}
			}
		}

		return $tpl->get();
	}

	/**
	 * Szerkeszt.
	 */
	private function action_editTournament() {
		$id = FunctionLibrary::getGetValue('id');
		$record = SiteDBHelper::getTournament($id, AdminBase::getCurrentEditlang(), true);
		foreach (array('date_from', 'date_to', 'deadline') as $key) {
			$record[$key] = TournamentHandler::formatDate($record[$key], "%Y-%m-%d");
		}
		$ttypes = SiteDBHelper::getTournamentTypesOfTournament($id);
		foreach ($ttypes as $ttype) {
			$record['types'][] = $ttype['id_tournamenttype'];
		}
		AdminPageHelper::action_edit('tournament', $record, "link_tournament");
	}

	/**
	 * Szerkesztés visszavonása.
	 */
	private function action_cancelTournament() {
		AdminPageHelper::action_cancel('tournament', "link_tournament");
	}

	/**
	 * Módosít.
	 */
	private function action_modTournament() {
		AdminPageHelper::action_mod($this, 'modTournament', 'tournament', 'tournament', "comp_mod_success", "comp_mod_err", "link_tournament");
	}

	/**
	 * Töröl.
	 */
	private function action_delTournament() {
		AdminPageHelper::action_del('delTournament', 'tournament', "comp_del_success", "comp_del_err", "link_tournament");
	}


	/**
	 * Hozzáad.
	 */
	private function action_addTournament() {
		AdminPageHelper::action_add($this, 'addTournament', 'newtournament', 'tournament', "comp_add_success", "comp_add_err", "link_tournament");
	}

	public function checkData($type, $values) {
		$p = new ParameterException();
		if ($type == 'add') {
			if (empty($values['newname'])) {
				$p->addError(TextHandler::getText("comp_err_emptyname"));
			}
			if (empty($values['newdatefrom'])) {
				$p->addError(TextHandler::getText("comp_err_emptydatefrom"));
			}
			if (empty($values['newdateto'])) {
				$p->addError(TextHandler::getText("comp_err_emptydateto"));
			}
		} else if ($type == 'mod') {
			if (empty($values['name'])) {
				$p->addError(TextHandler::getText("comp_err_emptyname"));
			}
			if (empty($values['date_from'])) {
				$p->addError(TextHandler::getText("comp_err_emptydatefrom"));
			}
			if (empty($values['date_to'])) {
				$p->addError(TextHandler::getText("comp_err_emptydateto"));
			}
		}
		if ($p->hasError()) throw $p;
	}

}
?>